package com.xbe.userSim.controller;

import com.xbe.common.annotation.Log;
import com.xbe.common.core.controller.BaseController;
import com.xbe.common.core.domain.AjaxResult;
import com.xbe.common.core.page.TableDataInfo;
import com.xbe.common.enums.BusinessType;
import com.xbe.userSim.domain.UserSim;
import com.xbe.userSim.service.IUserSimService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户卡号Controller
 *
 * @author liuzhiting
 * @date 2020-06-19
 */
@Controller
@RequestMapping("/userSim")
public class UserSimController extends BaseController
{
    private String prefix = "userSim";

    @Autowired
    private IUserSimService userSimService;

    /**
     * 查询用户卡号列表
     */
    @PostMapping("/list/{userId}")
    @RequiresPermissions("system:user:viewSim")
    @ResponseBody
    public TableDataInfo list(@PathVariable("userId") String userId)
    {
        startPage();
        UserSim userSim = new UserSim();
        userSim.setUserId(Long.parseLong(userId));
        List<UserSim> list = userSimService.selectUserSimListByUserId(userSim);
        return getDataTable(list);
    }

    // 卡号列表
    @GetMapping("/simList/{userId}")
    public String simList(ModelMap map, @PathVariable("userId") String userId)
    {

        map.put("userId", userId);

        return prefix + "/simList";
    }

    // 添加卡号
    @GetMapping("/addSim/{userId}")
    public String addSim(ModelMap map, @PathVariable("userId") String userId)
    {

        map.put("userId", userId);

        return prefix + "/addSim";
    }

    /**
     * 新增保存用户卡号
     */
    @Log(title = "用户卡号", businessType = BusinessType.INSERT)
    @PostMapping("/addSim")
    @RequiresPermissions("system:user:addSim")
    @ResponseBody
    public AjaxResult addSave(UserSim userSim)
    {
        //检验sim唯一
        UserSim userSimBySim = userSimService.selectUserSimBySim(userSim.getSim());
        if (userSimBySim != null)
        {
            return error("sim卡号已存在!");
        }
        return toAjax(userSimService.insertUserSim(userSim));
    }

    /**
     * 删除用户卡号
     */
    @Log(title = "用户卡号", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @RequiresPermissions("system:user:removeSim")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userSimService.deleteUserSimByIds(ids));
    }
}
